package com.example.seh.person.publish.errand;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.seh.R;
import com.example.seh.home.substitution.DeliverDTO;

import java.util.ArrayList;
import java.util.List;

public class MyErrandAdapter extends BaseAdapter {

    private Context context;
    private List<DeliverDTO> deliverDTOS = new ArrayList<>();
    private int item;

    public MyErrandAdapter(Context context, List<DeliverDTO> deliverDTOS, int item) {
        this.context = context;
        this.deliverDTOS = deliverDTOS;
        this.item = item;
    }

    @Override
    public int getCount() {
        if (deliverDTOS != null){
            return deliverDTOS.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (deliverDTOS != null){
            return deliverDTOS.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(item, null);
            viewHolder = new ViewHolder();
            viewHolder.goodsAddress = convertView.findViewById(R.id.errand_goodsAddress);
            viewHolder.myAddress = convertView.findViewById(R.id.errand_myAddress);
            viewHolder.goodsDesc = convertView.findViewById(R.id.errand_decs);
            viewHolder.errandState = convertView.findViewById(R.id.publish_state);

            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.goodsDesc.setText(deliverDTOS.get(position).getDeliverDesc());
        viewHolder.goodsAddress.setText(deliverDTOS.get(position).getAddressName());
        viewHolder.myAddress.setText(deliverDTOS.get(position).getReceiveName());
        if (deliverDTOS.get(position).getDeliverStatus() == 0){
            viewHolder.errandState.setText("未代取");
        }else {
            viewHolder.errandState.setText("已代取");
        }

        return convertView;
    }

    class ViewHolder {
        TextView goodsAddress,myAddress,goodsDesc,errandState;
    }
}
